package com.sqsxblog.navigation.fragment.view

import android.app.NotificationChannel
import android.app.NotificationManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.navigation.NavDeepLinkRequest
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.Navigation
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.navOptions
import com.sqsxblog.navigation.MainActivity
import com.sqsxblog.navigation.R


class FirstFragment : BaseFragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return inflater.inflate(R.layout.fragment_first,container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //由第一页打开第二页
        requireView().findViewById<Button>(R.id.jump).setOnClickListener {
            findNavController()
                .navigate(R.id.action_firstFragment_to_secondFragment)
        }
        //由第一页打开第二页，使用safeargs带上参数
        requireView().findViewById<Button>(R.id.jumpWithParamsSafe).setOnClickListener {

            findNavController()
                .navigate(
                    R.id.action_firstFragment_to_secondFragment,
                    //SecondFragmentArgs是自动生成的，不需要自己创建
                    //若报错，点击Build->Rebuild Project
                    SecondFragmentArgs("张三",24).toBundle()
                )
        }
        //由第一页打开第二页，不使用safeargs带上参数
        requireView().findViewById<Button>(R.id.jumpWithParams).setOnClickListener {
            val bundle = Bundle()
            bundle.putString("name","张三")
            bundle.putInt("age",24)
            findNavController()
                .navigate(
                    R.id.action_firstFragment_to_secondFragment,
                    bundle
                )
        }

        //创建推送，点击推送跳转指定fragment(显式深层链接)
        requireView().findViewById<Button>(R.id.deepLink1).setOnClickListener {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val importance = NotificationManager.IMPORTANCE_DEFAULT
                val channel = NotificationChannel("114514", "ChannelName", importance)
                channel.description = "description"
                val notificationManager = requireActivity().getSystemService(
                    NotificationManager::class.java
                )
                notificationManager.createNotificationChannel(channel)
            }

            val navDeepLinkBuilder = findNavController()
                .createDeepLink()
            //如果你不持有navController对象时，可以创建NavDeepLinkBuilder(context)对象替代上面的代码
            //val navDeepLinkBuilder = NavDeepLinkBuilder(context)
            val pendingIntent = navDeepLinkBuilder
                //指定你的navigation，你跳转的页面必须要在导航图创建过链接
                .setGraph(R.navigation.nav_main)
                //需要跳转的页面
                .setDestination(R.id.secondFragment)
                //你也可以这么添加需要跳转的页面，你添加多少个就按顺序打开多少个
//                .addDestination(R.id.thirdFragment)
//                .addDestination(R.id.fourthFragment)
                //非必选，但如果你在导航图添加了参数，哪怕目标页面没有做参数处理，这里是必选项。
                .setArguments(SecondFragmentArgs("张三",24).toBundle())
                //非必选，目标fragment的activity，一般不用写。
                .setComponentName(MainActivity::class.java)
                .createPendingIntent()

            Log.i("创建id",R.id.firstFragment.toString() + "," + R.id.secondFragment.toString())
            val builder = NotificationCompat.Builder(requireActivity(), "114514")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("标题")
                .setContentText("描述")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
            val notificationManager = NotificationManagerCompat.from(requireActivity())
            notificationManager.notify(114514, builder.build())
        }

        //隐式深层链接
        requireView().findViewById<Button>(R.id.deepLink2).setOnClickListener {
            val request = NavDeepLinkRequest(
                Uri.parse("sqsx://张三_24"),
                null,
                null
            )
            NavHostFragment.findNavController(this).navigate(request)
        }

        //监听消息，模拟onActivityResult
        findNavController()
            .currentBackStackEntry!!
            .savedStateHandle
            .getLiveData<String?>("key")    //这个key，可以为任意值，泛型内则为接收的数据类型
            .observe(viewLifecycleOwner) {
                Toast.makeText(requireContext(),it,Toast.LENGTH_SHORT).show()
            }

    }
}